package com.gdth.bank.activity;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gdth.bank.BankApplication;
import com.gdth.bank.R;
import com.gdth.bank.adapter.PhoneGridAdapter;
import com.gdth.bank.presenter.DzCpKhXxPosPresenter;
import com.gdth.bank.presenter.TXtBmPresenter;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.utils.MobileCheck;
import com.gdth.bank.utils.ServerUtil;
import com.gdth.bank.view.DzCpKhXxFjView;
import com.gdth.bank.view.DzCpKhXxPosView;
import com.gdth.bank.view.TXtBmView;
import com.gdth.bank.widget.BankDrawDownListTextView;
import com.gdth.bank.widget.BankEditText;
import com.gdth.bank.widget.PopupWindows;
import com.gdth.bank.widget.image.Bimp;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.database.bean.DzCpkhXxDjPos;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.database.bean.TAppYh;
import com.gdth.database.bean.TXtBm;
import com.gdth.model.Item;
import com.gdth.utils.RegularUtil;
import com.gdth.utils.ToolUtil;
import com.gdth.utils.UUIDUtil;
import com.gdth.widget.dialog.BaseToast;

public class DzCpKhXxPosGeRenXxMxXgActivity  extends BaseActivity implements DzCpKhXxPosView ,DzCpKhXxFjView,TXtBmView{
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left, click="finishClick") TextView txt_header_left;
	@ViewInject(id=R.id.header_btn_right, click="saveClick") TextView txt_header_right;
    @ViewInject(id=R.id.dzcpkhxx_linear_nav) LinearLayout linear_nav;
	@ViewInject(id=R.id.dzcpkh_xm) BankEditText txt_khxm;
	@ViewInject(id=R.id.dzcpkh_sfzhm) BankEditText txt_khsfzhm;
	@ViewInject(id=R.id.dzcpkh_sjhm) BankEditText txt_khsjhm;
	@ViewInject(id=R.id.dzcpkh_zdh) BankEditText dzcpkh_zdh;
	@ViewInject(id=R.id.dzcpkh_shh) BankEditText dzcpkh_shh;
	@ViewInject(id=R.id.dzcpkh_khckzh) BankEditText dzcpkh_khckzh;
	@ViewInject(id=R.id.dzcpkh_sbzt) BankEditText dzcpkh_sbzt;
	@ViewInject(id=R.id.dzcpkh_pos_jg) BankDrawDownListTextView drawDownList_etc_khjg;
	@ViewInject(id=R.id.dzcpkh_zjlx) BankDrawDownListTextView drawDownList_etc_khzjlx;
	@ViewInject(id=R.id.noScrollgridviewpos, itemClick="onItemClick") GridView gridview_phone;
	List<Item> zjList = new ArrayList<Item>();
	List<Item> jgList = new ArrayList<Item>();
	PhoneGridAdapter adapter;
	PopupWindows mPopupWindows;
	Long id;
	DzCpkhXxDjPos  mDzCpkhXxDjPos;
	TAppFjxxb  mDzCpkhXxDjFj;
	DzCpKhXxPosPresenter mDzCpKhXxPosPresenter;
	TXtBmPresenter  mTXtBmPresenter;
	String appuuid,zt;
	RegularUtil checkCard;
	String  isCheck,scbs,sqrSfz;
	Date posSqsj;
	int Zdlx;
	/** 提示框 */
    ProgressDialog mProgressDialog;
    MobileCheck  mobileCheck;
	boolean idMoblieCheck;
	
	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dzcp_pos);
		initializeView();
		init();
		
	}
	public void initializeView(){
		Intent intent = this.getIntent();
		appuuid = intent.getStringExtra("appuuid");
		zt = intent.getStringExtra("zt");
		scbs = intent.getStringExtra("scbs");
		if(scbs==null){
			scbs = "0";
		}
		txt_header_title.setText(getResources().getString(R.string.param189));
		txt_khxm.setLabel(R.string.dzcpxxdj_kh_xm);
//		txt_khxm.setHint(R.string.dzcpxxdj_kh_xm_hint);
		txt_khxm.setImage(null);
		txt_khsfzhm.setLabel(R.string.dzcpxxdj_kh_sfzhm);
//		txt_khsfzhm.setHint(R.string.dzcpxxdj_kh_sfzhm_hint);
		txt_khsfzhm.setImage(null);
		txt_khsjhm.setLabel(R.string.dzcpxxdj_kh_sjhm);
//		txt_khsjhm.setHint(R.string.dzcpxxdj_kh_sjhm_hint);
		txt_khsjhm.setImage(null);
		dzcpkh_zdh.setLabel(R.string.dzcpxxdj_kh_zdh);
//		dzcpkh_zdh.setHint(R.string.dzcpxxdj_kh_zdh_hint);
		dzcpkh_zdh.setImage(null);
		dzcpkh_zdh.setVisibility(View.GONE);
		dzcpkh_shh.setLabel(R.string.dzcpxxdj_kh_shh);
//		dzcpkh_shh.setHint(R.string.dzcpxxdj_kh_shh_hint);
		dzcpkh_shh.setImage(null);
		dzcpkh_shh.setVisibility(View.GONE);
		dzcpkh_khckzh.setLabel(R.string.dzcpxxdj_kh_ckzh);
//		dzcpkh_khckzh.setHint(R.string.dzcpxxdj_kh_ckzh_hint);
		dzcpkh_khckzh.setImage(null);
		drawDownList_etc_khjg.setLabel(R.string.dzcpxxdj_kh_jg);
		drawDownList_etc_khzjlx.setLabel(R.string.dzcpxxdj_kh_zjlx);
		txt_header_right.setText("保存");
		txt_header_right.setBackgroundDrawable(null);
		dzcpkh_sbzt.setImage(null);
		dzcpkh_sbzt.setLabel(R.string.dzcpxxdj_kh_sbzt);
		dzcpkh_sbzt.setVisibility(View.GONE);
		
		
		Item item = new Item();
		item.setKey("1");
		item.setValue(getResources().getString(R.string.param190));
		zjList.add(item);
		Item item1 = new Item();
		item1.setKey("2");
		item1.setValue(getResources().getString(R.string.param191));
		zjList.add(item1);
		drawDownList_etc_khzjlx.setItem(zjList);
		drawDownList_etc_khzjlx.setValue("1");
		
		mTXtBmPresenter = new TXtBmPresenter(this);
		mTXtBmPresenter.getAllBm("");
		checkCard = new RegularUtil();
		mProgressDialog = new ProgressDialog(this);
		mobileCheck = new MobileCheck();
		
	}
	public void init() {
		Bimp.getInstance().reset();
		adapter = new PhoneGridAdapter(this,scbs);
		gridview_phone.setAdapter(adapter);
		mDzCpKhXxPosPresenter= new DzCpKhXxPosPresenter(this);
		
		
	}
	public void saveClick(View arg0){
		mDzCpkhXxDjPos = new DzCpkhXxDjPos();
		mDzCpkhXxDjFj = new TAppFjxxb();
		if(id!=null){
			mDzCpkhXxDjPos.setId(id);
			mDzCpkhXxDjPos.setKhMc(txt_khxm.getValue().toString());
			mDzCpkhXxDjPos.setKhZjh(txt_khsfzhm.getValue().toString());
			mDzCpkhXxDjPos.setKhSjhm(txt_khsjhm.getValue().toString());
			mDzCpkhXxDjPos.setBmbh(drawDownList_etc_khjg.getkey().toString());
			mDzCpkhXxDjPos.setAppUuid(appuuid);
			mDzCpkhXxDjPos.setKhCkzh(dzcpkh_khckzh.getValue().toString());
//			mDzCpkhXxDjPos.setShh(dzcpkh_shh.getValue().toString());
//			mDzCpkhXxDjPos.setZdh(dzcpkh_zdh.getValue().toString());
			mDzCpkhXxDjPos.setSbZt(0);
			mDzCpkhXxDjPos.setXgzt(0);
			mDzCpkhXxDjPos.setZdlx(Zdlx);
			mDzCpkhXxDjPos.setSqSj(posSqsj);
//			mDzCpkhXxDjPos.setXgSj(new Date());
			mDzCpkhXxDjPos.setSqrSfz(sqrSfz);
			mDzCpkhXxDjPos.setKhZjlx(Integer.parseInt(drawDownList_etc_khzjlx.getkey().toString()));
			/*String json = SharedPreferencesUtil.getSharePreferencesByKey(this, AppUtil.SHARE_LOGIN_TAG, AppUtil.SHARE_USER);*/
			TAppYh user = BankApplication.mInstance.getCurrentUser();
//			mDzCpkhXxDjPos.setXgrSfz(user.getZh());
//			mDzCpKhXxPosPresenter.saveDzcpkhxxpos(mDzCpkhXxDjPos);
		
		}else{
			String appuuid = UUIDUtil.getUUID();
			mDzCpkhXxDjPos.setAppUuid(appuuid);
			mDzCpkhXxDjPos.setZdlx(1);
			mDzCpkhXxDjFj.setWybs(appuuid);
			mDzCpkhXxDjPos.setBmbh(drawDownList_etc_khjg.getkey().toString());
		    mDzCpkhXxDjPos.setKhZjlx(Integer.parseInt(drawDownList_etc_khzjlx.getkey().toString()));
			mDzCpkhXxDjPos.setKhZjh(txt_khsfzhm.getValue().toString());
			mDzCpkhXxDjPos.setKhMc(txt_khxm.getValue().toString());
			mDzCpkhXxDjPos.setKhSjhm(txt_khsjhm.getValue().toString());
			mDzCpkhXxDjPos.setSqSj(new Date());
			mDzCpkhXxDjPos.setSbZt(0);
			mDzCpkhXxDjPos.setXgzt(0);}
			/*String json = SharedPreferencesUtil.getSharePreferencesByKey(this, AppUtil.SHARE_LOGIN_TAG, AppUtil.SHARE_USER);*/
			TAppYh user = BankApplication.mInstance.getCurrentUser();
            List<TAppFjxxb> mList = Bimp.getInstance().getDrr();
			List<TAppFjxxb> fjxxb = new ArrayList<TAppFjxxb>();
			for (TAppFjxxb str : mList) {
				String path = str.getAppdz();
				int sczt = str.getSczt();
				if(sczt==0){
				TAppFjxxb mTAppFjxxb = new TAppFjxxb();
				mTAppFjxxb.setAppdz(path);
				mTAppFjxxb.setWybs(appuuid);
				mTAppFjxxb.setCjsj(new Date());
				mTAppFjxxb.setGnmk(3);
				mTAppFjxxb.setSczt(0);
				mTAppFjxxb.setWjlx(path.substring(path.lastIndexOf(".")+1, path.length()));
				mTAppFjxxb.setSfz(user.getZh());
				mTAppFjxxb.setUuid(str.getUuid());
				mTAppFjxxb.setWjmc(path.substring(path.lastIndexOf("/")+1, path.length() ));
				mTAppFjxxb.setZdlx(1);
				fjxxb.add(mTAppFjxxb);
				}
			}
//			mDzCpkhXxDjPos.getFjxxs().addAll(fjxxb);
			mDzCpkhXxDjPos.setFjxxs(fjxxb);
			if(txt_khxm.getValue().toString().equals("") || txt_khsfzhm.getValue().toString().equals("") || txt_khsjhm.getValue().toString().equals("") || drawDownList_etc_khjg.getValue().toString().equals("")  || drawDownList_etc_khzjlx.getValue().toString().equals("") || dzcpkh_khckzh.getValue().toString().equals("") ){
				Toast.makeText(this, getResources().getString(R.string.param192), Toast.LENGTH_SHORT).show();
			}else if(dzcpkh_khckzh.getValue().toString().length() >20){
				 Toast.makeText(this, getResources().getString(R.string.param193), Toast.LENGTH_SHORT).show();
			 }
			else{
				 if(txt_khsfzhm.getValue().toString()!=null){
					 try {
					 isCheck=checkCard.IDCardValidate(txt_khsfzhm.getValue().toString());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				 }
				 idMoblieCheck =mobileCheck.isMobileNO(txt_khsjhm.getValue().toString());
				 if(isCheck.toString().trim().equals("")  &&  idMoblieCheck==true && drawDownList_etc_khzjlx.getkey().toString().equals("1") ){
					 mDzCpKhXxPosPresenter.saveDzcpkhxxpos(mDzCpkhXxDjPos);
					 //					 mDzCpKhXxPosPresenter.saveDzcpkhxxpos(mDzCpkhXxDjPos);
				 }else if(idMoblieCheck==true && drawDownList_etc_khzjlx.getkey().toString().equals("2")){
					 mDzCpKhXxPosPresenter.saveDzcpkhxxpos(mDzCpkhXxDjPos);
//					 mDzCpKhXxPosPresenter.saveDzcpkhxxpos(mDzCpkhXxDjPos);
				 }
				 else{
					 Toast.makeText(this, getResources().getString(R.string.param194), Toast.LENGTH_SHORT).show();
				 }
				
			}
//			if(txt_khxm.getValue().toString().equals("") || txt_khsfzhm.getValue().toString().equals("") || txt_khsjhm.getValue().toString().equals("") || drawDownList_etc_khjg.getValue().toString().equals("")  || drawDownList_etc_khzjlx.getValue().toString().equals("")  ){
//				Toast.makeText(this, "请将信息填写完整", Toast.LENGTH_SHORT).show();
//			}else{
//				 if(txt_khsfzhm.getValue().toString()!=null){
//					 try {
//					 isCheck=checkCard.IDCardValidate(txt_khsfzhm.getValue().toString());
//					} catch (ParseException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				 }
//				 if(isCheck.toString().trim().equals("")){
//					 mDzCpKhXxPosPresenter.saveDzcpkhxxpos(mDzCpkhXxDjPos);
//				 }else{
//					 Toast.makeText(this, "身份证信息不合法，请重新输入", Toast.LENGTH_SHORT).show();
//				 }
//				
//			}
		}
		
		
	
	private void saveKhXx() {
		Bimp.getInstance().reset();
		finish();
	}
	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		mProgressDialog.setMessage(msg);
		mProgressDialog.show();
	}
	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		mProgressDialog.hide();
	}
	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		BaseToast.getInstance().ShowSystemToast(this, msg);
	}
	@Override
	public void updateViewpos(int type, Object Obj) {
		// TODO Auto-generated method stub
		if (type == ServerUtil.server_save){
			showMessage(getResources().getString(R.string.param195));
		} else if (type == mDzCpKhXxPosPresenter.loadDzCpKhXx){
			/*List<DzCpkhXxDjPos>	mDzCpkhXxposList = (List<DzCpkhXxDjPos>) Obj;
			DzCpkhXxDjPos  mDzCpkhXxDjPos=mDzCpkhXxposList.get(0);*/
		    DzCpkhXxDjPos  mDzCpkhXxDjPos=(DzCpkhXxDjPos) Obj;
			id=mDzCpkhXxDjPos.getId();
			posSqsj = mDzCpkhXxDjPos.getSqSj();
			sqrSfz  = mDzCpkhXxDjPos.getSqrSfz();
			Zdlx = mDzCpkhXxDjPos.getZdlx();
			txt_khxm.setValue(mDzCpkhXxDjPos.getKhMc());
			txt_khsfzhm.setValue(mDzCpkhXxDjPos.getKhZjh());
			txt_khsjhm.setValue(mDzCpkhXxDjPos.getKhSjhm());
			dzcpkh_khckzh.setValue(mDzCpkhXxDjPos.getKhCkzh());
			dzcpkh_shh.setValue(mDzCpkhXxDjPos.getShh());
			dzcpkh_zdh.setValue(mDzCpkhXxDjPos.getZdh());
			drawDownList_etc_khjg.setValue(mDzCpkhXxDjPos.getBmbh().toString());
			drawDownList_etc_khzjlx.setValue(mDzCpkhXxDjPos.getKhZjlx().toString());
		    int sbzt= mDzCpkhXxDjPos.getSbZt();
		    dzcpkh_sbzt.setValue(mDzCpkhXxDjPos.getSbZtMc());
		    drawDownList_etc_khzjlx.mEditText.setEnabled(false);
		    drawDownList_etc_khjg.mEditText.setEnabled(false);
		    txt_khsfzhm.mEditText.setEnabled(false);
//		    if(sbzt==0){
//		    	dzcpkh_sbzt.setValue("申请登记");
//		       }else if(sbzt==1){
//		    	   dzcpkh_sbzt.setValue("已成交");
//		       }else if(sbzt==2){
//		    	   dzcpkh_sbzt.setValue("申报中");
//		       }else if(sbzt==3){
//		    	   dzcpkh_sbzt.setValue("待领取");
//		       }else if(sbzt==4){
//		    	   dzcpkh_sbzt.setValue("已作废");
//		       }
			
			/*List<TAppFjxxb> fjList = mDzCpkhXxDjPos.getFjxxs();
			for(int i=0;i<fjList.size();i++){
				TAppFjxxb tAppFjxxb = fjList.get(i);
				Bimp.getInstance().getDrr().add(tAppFjxxb);
			}
			updateGridView();*/
			mDzCpkhXxDjPos.getFjxxs();
			setFjxxList(mDzCpkhXxDjPos.getFjxxs());
			
			
		}
	}
	@Override
	public void updateListpos(List<DzCpkhXxDjPos> mList) {
		// TODO Auto-generated method stub
		
	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
		if (arg2 == Bimp.getInstance().getBmp().size()){
			mPopupWindows = new PopupWindows(this, gridview_phone,1);
		} else {
			Intent intent = new Intent(this, PhotoActivity.class);
			intent.putExtra("ID", arg2);
			this.startActivity(intent);
		}
	}
	public void updateGridView(){
		adapter.update();
	}
	public void setPhone(){
		if (Bimp.getInstance().getCount() < 9){
			if (mPopupWindows.savePath != null){
				/*String path = mPopupWindows.savePath;
				TAppFjxxb mTAppFjxxb = new TAppFjxxb();
				mTAppFjxxb.setAppdz(path);
				mTAppFjxxb.setSczt(-1);
				Bimp.getInstance().getDrr().add(mTAppFjxxb);
				
				updateGridView();*/
				String fileName = mPopupWindows.uuid + "." + mPopupWindows.wjlx;
				TAppFjxxb mTAppFjxxb = new TAppFjxxb();
				mTAppFjxxb.setSczt(0);
				
				mTAppFjxxb.setUuid(mPopupWindows.uuid);
				mTAppFjxxb.setWjlx(mPopupWindows.wjlx);
				mTAppFjxxb.setWjmc(fileName);
				//获取本地保存路径
				mTAppFjxxb.setAppdz(ToolUtil.getFilePath(fileName));
				Bimp.getInstance().getDrr().add(mTAppFjxxb);
				
				updateGridView();
			}
		}
	}
	
	
	@Override  
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == AppUtil.activity_photo_requestcode) {
			if(resultCode == Activity.RESULT_CANCELED){
				
			}else{
				this.setPhone();
			}
		}
		if (requestCode == AppUtil.activity_picture_requestcode && resultCode == AppUtil.activity_picture_resultcode) {
			updateGridView();
		}
	}
	@Override
	public void updateViewFj(int type, Object Obj) {
		// TODO Auto-generated method stub
		/*if (type == ServerUtil.server_save){
			showMessage("保存成功");
		} else if (type == mDzCpKhXxFjPresenter.loadDzCpKhXx){
			List<TAppFjxxb>	mTAppFjxxbList = (List<TAppFjxxb>) Obj;
			TAppFjxxb  mTAppFjxxb=mTAppFjxxbList.get(0);
			id=mTAppFjxxb.getId();
		    String s=mTAppFjxxb.getAppdz();
		    String ss= s.replace("[", "").trim();
		    String sss=ss.replace("]", "").trim();
		    String d[] = sss.split(",");
		    for (int i = 0; i < d.length; i++) {
		        Bimp.getInstance().getDrr().add(d[i].trim());
		        
		            }
		    updateGridView();
			
		}*/
	}
	@Override
	public void updateListBm(List<TXtBm> mList) {
		// TODO Auto-generated method stub
		for(int i=0;i<mList.size();i++){
			Item item= new Item();
			item.setKey(mList.get(i).getBh().toString());
			item.setValue(mList.get(i).getJc());
			jgList.add(item);
		}
		drawDownList_etc_khjg.setItem(jgList);
		if(appuuid!=null){
			mDzCpKhXxPosPresenter.serachDzcpkhxxpos(appuuid);
		}
	}
	@Override
	public void success() {
		// TODO Auto-generated method stub
		saveKhXx();
	}
	
	public void setFjxxList(List<TAppFjxxb> fjxxList) {
		for (TAppFjxxb fjxx : fjxxList) {
			fjxx.setSczt(1);
		String wjlx=fjxx.getWjmc();
		String wjlxx = wjlx.substring(wjlx.lastIndexOf(".") + 1, wjlx.length());
		fjxx.setWjlx(wjlxx);
		fjxx.setSczt(1);
		fjxx.setAppdz(ToolUtil.getFilePath(fjxx.getUuid() +"."+wjlxx));
		fjxx.setGnmk(2);
			Bimp.getInstance().getDrr().add(fjxx);
		}
		updateGridView();
	}
	@Override
	public void scsuccess() {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setClass(DzCpKhXxPosGeRenXxMxXgActivity.this, DzCpPosGeRenXxActivity.class);
		startActivity(intent);
	}
	@Override
	public void jyZhj(String arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void refreshData(List<DzCpkhXxDjPos> mList) {
		// TODO Auto-generated method stub
		
	}
}
